package com.kgd.news.controller.admin;

import com.kgd.news.entity.SysMenu;
import com.kgd.news.entity.SysRoleMenu;
import com.kgd.news.entity.SysUser;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.Constants;
import com.kgd.news.utils.StringUtilsWx;

import java.util.ArrayList;
import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/14
 * @Time: 下午7:38
 */
public class AdminController extends BaseController {

    public void index() {
        render("index.html");
    }

    /**
     * 后台首页
     */
    public void platform() {
        if (getSessionAttr(Constants.request_cUser) == null) {
            render("index.html");
            return;
        }
        String menuOpenIndex = getPara("menuOpenIndex");
        String menuIds = getMenuIdByRoleID();
        List<SysMenu> menus = queryMenu(1, menuIds);
        setAttr("menuList", menus);
        setAttr("menuOpenIndex", menuOpenIndex);
        render("main.html");
    }

    private List<SysMenu> queryMenu(int flag, String parentid) {
        String sql = "select * from sys_menu where  1 = 1 ";
        if (flag == 1) {
            sql += " and id in (" + parentid + ")";
        } else {
            sql += " and parentid = ? order by m_order desc";
            return SysMenu.dao.find(sql, parentid);
        }
        sql += " order by m_order desc";
        List<SysMenu> menus = SysMenu.dao.find(sql);
        List<SysMenu> data = new ArrayList<>();
        for (SysMenu menu : menus) {
            if (menu.getParentid() == null || menu.getParentid() == 0) {
                List<SysMenu> list = queryMenu(2, menu.getId() + "");
                menu.setChildMenu(list);
            }
            data.add(menu);
        }
        return data;
    }

    public void resetPwd() {
        render("reset_pwd.html");
    }

    public void savePwd() {
        String srcPwd = getPara("src_pwd");
        if (StringUtilsWx.isEmpty(srcPwd)) {
            setAttr("error_msg", "请输入原始密码");
            render("reset_pwd.html");
            return;
        }
        String srcPwd1 = getPara("src_pwd1");

        if (StringUtilsWx.isEmpty(srcPwd1)) {
            setAttr("error_msg", "请输入新密码");
            render("reset_pwd.html");
            return;
        }

        String srcPwd2 = getPara("src_pwd2");
        if (StringUtilsWx.isEmpty(srcPwd2)) {
            setAttr("error_msg", "请再次输入新密码");
            render("reset_pwd.html");
            return;
        }

        if (!srcPwd1.equals(srcPwd2)) {
            setAttr("error_msg", "两次输入的密码不一致");
            render("reset_pwd.html");
            return;
        }

        SysUser su = getSessionUser();
        if (!srcPwd.equals(su.getPasswd())) {
            setAttr("error_msg", "输入的原密码有错误");
            render("reset_pwd.html");
            return;
        }
        su.setPasswd(srcPwd1);
        boolean flag = su.update();
        if (flag) {
            cacheUser(null);
            setAttr("error_msg", "密码重置成功");
            render("reset_result.html");
        } else {
            setAttr("error_msg", "系统错误，请稍候再试");
            render("reset_pwd.html");
        }
    }

    //登出
    public void exitPwd() {
        cacheUser(null);
        render("index.html");
    }
    /**
     * 递归查询菜单

     private List<SysMenu> queryMenu(String menuIds) {
     String sql = "select * from sys_menu where 1 = 1 and id in (" + menuIds + ") order by m_order desc";
     List<SysMenu> menus = SysMenu.dao.find(sql);
     List<SysMenu> data = new ArrayList<>();
     for (SysMenu menu : menus) {
     if(menu.getParentid() == null){
     data.add(menu);
     }else{
     if (data != null) {
     for (SysMenu sm: data) {
     if(sm.getId() == menu.getParentid()){
     List<SysMenu> slist = sm.getChildMenu();
     if(slist == null){
     slist = new ArrayList<SysMenu>();
     }
     slist.add(menu);
     sm.setChildMenu(slist);
     }
     }
     }
     }
     }
     return data;
     }*/


    /**
     * 通过角色ID查询菜单
     */
    private String getMenuIdByRoleID() {
        //通过setSessionAttr(key, value)可以向 session 中存放数据，getSessionAttr(key)存储登陆信息
        SysUser su = getSessionUser();
        String sql = "select * from sys_role_menu where roleid = " + su.getId();
        //String sql = "select * from sys_role_menu where roleid = 4";
        List<SysRoleMenu> srmList = SysRoleMenu.dao.find(sql);
        String ids = "";
        for (SysRoleMenu roleMenu : srmList) {
            ids += roleMenu.getMenuid() + ",";
        }
        return ids.substring(0, ids.length() - 1);
    }


    @Override
    public void list() {

    }

    @Override
    public void edit() {

    }

    @Override
    public void save() {

    }

    @Override
    public void del() {

    }

}
